import re

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework import serializers
from user.models import UserModel
from django_redis import get_redis_connection
class CoustomTokenObtainPairSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls,user):
        token = super().get_token(user)
        # Add custom claims
        token['username'] = str(user.username)
        token['money'] = str(user.money)
        token['avatar']=str(user.avatar)
        token['credit'] = str(user.credit)
        # ...

        return token
class  RegisterSerializer(serializers.ModelSerializer):
    re_password=serializers.CharField(required=True,write_only=True,min_length=6,max_length=16)
    sms_code=serializers.CharField(required=True,write_only=True)
    token=serializers.CharField(read_only=True)
    refresh=serializers.CharField(read_only=True)

    class Meta:
        model = UserModel
        fields = ['mobile','password','token','refresh','re_password','sms_code']
        extra_kwargs = {
            'mobile':{
                'required':True,
                "write_only":True,
                'min_length':11,
                'max_length':11
            },
            'password': {
                'required': True,
                'write_only': True,
                'min_length' :6,
                'max_length' : 16
            }
        }
    def validate(self, attrs):
            mobile=attrs.get('mobile',None)
            # 判断手机号
            if not re.match(r"^1[3-9]\d{9}$",mobile):
                raise serializers.ValidationError(detail='手机号格式错误')
            print('------手机号校验通过-------')
            # 判断两次输入的密码
            password = attrs.get('password')
            re_password = attrs.get('re_password')
            if password != re_password:
                raise serializers.ValidationError(detail='两次密码不一致，请重新输入',code='password')
            print('------密码校验通过-------')
            # -------------------redis:短信验证码------------------
            sms_code = attrs.get('sms_code')
            # 验证码操作 稍后开发
            # 默认验证码是正确
            redis = get_redis_connection('sms_code')
            code = redis.get(f'sms_{mobile}')
            # redis中获取到数据类型,都是字节类型的
            print('redis中保存的短信验证码:', code)
            if code == None:
                raise serializers.ValidationError(detail='验证码过期,请重新获取验证码', code="sms_code")
            if code.decode() != sms_code:
                raise serializers.ValidationError(detail='验证码错误,请重新输入', code='sms_code')

            redis.delete(f'sms_{mobile}')

            # -------------------redis:短信验证码------------------

            # 程序健壮性考虑
            # 判断一次用户是否存在
            try:
                UserModel.objects.get(mobile__exact=mobile)
                raise serializers.ValidationError(detail='用户已经存在了...', code='mobile')
            except UserModel.DoesNotExist:
                print('注册信息,不存在于数据,可以正常执行注册操作.所有数据校验通过')
            attrs.pop('re_password')
            attrs.pop('sms_code')
            return attrs
    def create(self, validated_data):
        mobile=validated_data.get('mobile')
        password=validated_data.get('password')
        user=UserModel.objects.create_user(
            mobile=mobile,
            password=password,
            username=mobile,
            money=0.0,
            credit=0,
            avatar='avatar/2021/default.jpg'
        )
        #返回数据
        refresh =CoustomTokenObtainPairSerializer.get_token(user=user)
        user.refresh=str(refresh)
        user.token=str(refresh.access_token)
        return user




